<?php
/**
 * EngWord
 *
 * Класс EngWord обрабатывает данные, связанные с конкретными словами
 *
 * @author Дмитрий Кузьма <kouzma.d@gmail.com>
 * @version 1.1
 */
class EngWord extends ActiveRecord {
	public $tableName = 'eng_word_1';
	public $pkName = 'ID_eng_word_1';
	
	public $arrPronouns = array('I', 'you', 'we', 'he', 'she', 'it', 'they', 'my', 'your', 'our', 'their', 'his', 'her', 'its', 'me', 'us', 'them', 'him', 'yours'); // type = 1
	public $arrArticles = array('a', 'an', 'the'); // type = 2 
	public $arrNumbers = array( // type = 3
		'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 
		'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen',
		'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety', 'hundred',
		'first', 'second', 'third', 'fourth'
	);
	public $arrPrepositions = array( // type = 4
		'on', 'at', 'of', 'from', 'in', 'with', 'for', 'but', 'out', 'to', 'about', 'up', 'by', 'off', 'into', 'under',
		'oh', 'well', 'uh', 'um', 'huh', 'wow', 'Ooh', 'ew', 'Ohhhhh',
		'so', 'because', 'just', 'as',
		'and', 'or', 'not', 'if', 'too',
		'yes', 'no', 'Yeah', 
		'this', 'that', 'these', 'those'
	);
	public $arrQuestions = array( // type = 5
		'what', 'where', 'when', 'who', 'whose', 'whom', 'how', 'why', 
	);
	public $arrVerbs = array( // type = 6
		'can', 'could', 'may',
		'had', 'have', 'has', 'haven\'t',  'hasn\'t',  'hadn\'t', 'having',
		'be', 'is', 'am', 'are', 'was', 'were', 'been', 'aren\'t', 'weren\'t', 'being',
		'will', 'would', 'shal', 'should',
		'do', 'done', 'does', 'don\'t', 'didn\'t', 'did', 'doesn\'t', 'doing', 
			
		"I'm", "she's", "he's", "it's", "you're", "That's",
		"we're", "I've", "There's", "what's", "I'll", "They're", "I'd", "let's", "wasn't", "We'll", "isn't"
	);
	public $arrNames = array( // type = 7
		'Ross', 'Monica', 'Joey', 'Rachel', 'Chandler', 'Marcel', 'Carol', 'Phoebe', 'Pheebs', 'Barry', 'Susan', 'Mindy', 'Janice', 'Rach', 'Paolo', 'Brian', 'Alan', 'Ben', 'Paul', 'Geller', 'Nana', 'Ethan',
		'Greene', 'Bob', 'Helen', 'Jack', 'Bobby', 'Lowell', 'Ursula', 'San', 'Stalin', 'Ronni', 'WENUS', 'Luisa', 'Boobies', 'Tribbiani', 'Aruba', 'Danielle', 'Joe', 'Jordy', 'Heckles', 'Diego', 'Debbie',
		'Rick', 'Pop', 'George'
	);
	

	/**
	 * Метод задает стандартные типы для предварительно заданных слов
	 */
	public function setStandardTypes() {
		Service_Debug::setDebug();
		$arrAllTypes = array();
		$arrAllTypes[1] = $this->arrPronouns;
		$arrAllTypes[2] = $this->arrArticles;
		$arrAllTypes[3] = $this->arrNumbers;
		$arrAllTypes[4] = $this->arrPrepositions;
		$arrAllTypes[5] = $this->arrQuestions;
		$arrAllTypes[6] = $this->arrVerbs;
		$arrAllTypes[7] = $this->arrNames;

		$objDB = Service_Database::instance();
		foreach ($arrAllTypes as $type => $arrWords) {
			foreach ($arrWords as &$word) $word = Service_Escape::escapeString($word);
			$stmt = "UPDATE `eng_word_1` SET `word_type` = '" . $type . "' WHERE `word` IN ('" . implode("', '", $arrWords) . "')";
			echo $type;
			echo ' - ';
			echo $objDB->query($stmt);
			echo '<br />';
		}
	}
	
	/**
	 * Получает список популярных предложений
	 */
	public function getPopularSenses($limit = 10) {
		Service_Debug::setDebug();
		if (!$this->id) return array();
		$objDB = Service_Database::instance();
		$stmt = "
			SELECT `eng_sense`.* FROM `eng_sense_word_1`
			INNER JOIN `eng_sense` USING(`ID_eng_sense`) 
			WHERE `ID_eng_word_1` = '" .$this->id . "'
			ORDER BY `eng_sense_word_1`.`amount` DESC
			LIMIT 0, " . intval($limit) . " 
		";
		return $objDB->select($stmt);
	}
	
	/**
	 * Обновляет слова, содержащие внутри себя кавычку
	 */
	public function updateWordsWithQuptes() {
		Service_Debug::setDebug();
		$objDB = Service_Database::instance();
		$stmt = "SELECT `eng_word_1`.* FROM `eng_word_1` 
		WHERE `word` LIKE '%\\'%'	AND `ID_parent_word` IS NULL AND word_type IS NULL
		ORDER BY `ID_parent_word` DESC, `eng_word_1_amount` DESC ";
		$arrWords = $objDB->select($stmt);
		if (!$arrWords) return false;
		foreach ($arrWords as $arrWord) {
			$strWord = $arrWord['word'];
			$strWordBeforeQuote = substr($strWord, 0, strpos($strWord, "'"));
			$stmt = "SELECT `eng_word_1`.`ID_eng_word_1` FROM `eng_word_1` WHERE `word` = '" . $strWordBeforeQuote . "'";
			$shortWordId = $objDB->selectSimpleField($stmt);
			if ($shortWordId) {
				$stmt = "UPDATE `eng_word_1` SET `ID_parent_word` = '" . $shortWordId . "' WHERE `ID_eng_word_1` = '" . $arrWord['ID_eng_word_1'] . "'";
				$objDB->query($stmt);
			}
			
		}
	}
	
	/**
	 * Проверяет слово на совпадение со словоформами
	 */
	public function checkWordsForCoincidences(array $arrWords, $suffix) {
		$objDB = Service_Database::instance();
		if (!$arrWords) return false;
		$stmtIn = "";
		$stmtNotIn = "";
		$i = 0;
		foreach ($arrWords as $arrWord) {
			if ($i++) {
				$stmtIn .= ", ";
				$stmtNotIn = ", ";
			}
			
			$strWord = $arrWord['word'];
			$strShortWord = substr($strWord, 0, strlen($strWord) - strlen($suffix));
			$stmtIn .= "'" . $strShortWord . "'";
			$stmtNotIn .= "'" . $strWord . "'";
			
		}
		//Service_ArrayConverter::getColumnAsString($arrIn, $columnName)
		$stmt = "SELECT * FROM `eng_word_1`
		WHERE `eng_word_1`.`word` IN (" . $stmtIn . ") ";
		$arrWords = $objDB->select($stmt);
	}
	
	
	public function checkWordForCoincidences(array $arrWord, $suffix, $alternative, $alternative2) {
		$objDB = Service_Database::instance();
		if (strlen($arrWord['word']) < 4) return false;
		
		//Service_ArrayConverter::getColumnAsString($arrIn, $columnName)
		$stmt = "SELECT * FROM `eng_word_1`
		WHERE `eng_word_1`.`word` = '" . substr($arrWord['word'], 0, strlen($arrWord['word']) - strlen($suffix)) . "'";
		$arrRootWord = $objDB->selectOne($stmt);
		if ($arrRootWord) {
			$stmt = "UPDATE `eng_word_1` SET `eng_word_1`.`word_sub_type` = '" . $suffix . "', `eng_word_1`.`ID_root_word` = '" . $arrRootWord['ID_eng_word_1'] . "' WHERE `eng_word_1`.`ID_eng_word_1` = '" . $arrWord['ID_eng_word_1'] . "'";
			$objDB->query($stmt);
		}
		else {
			$stmt = "SELECT * FROM `eng_word_1`
			WHERE `eng_word_1`.`word` = '" . substr($arrWord['word'], 0, strlen($arrWord['word']) - strlen($suffix)) . $alternative . "'";
			$arrRootWord = $objDB->selectOne($stmt);
			if ($arrRootWord) {
				$stmt = "UPDATE `eng_word_1` SET `eng_word_1`.`word_sub_type` = '" . $suffix . "', `eng_word_1`.`ID_root_word` = '" . $arrRootWord['ID_eng_word_1'] . "' WHERE `eng_word_1`.`ID_eng_word_1` = '" . $arrWord['ID_eng_word_1'] . "'";
				$objDB->query($stmt);
			}
			else {
				$stmt = "SELECT * FROM `eng_word_1`
				WHERE `eng_word_1`.`word` = '" . substr($arrWord['word'], 0, strlen($arrWord['word']) - strlen($suffix)) . $alternative2 . "'";
				$arrRootWord = $objDB->selectOne($stmt);
				if ($arrRootWord) {
					$stmt = "UPDATE `eng_word_1` SET `eng_word_1`.`word_sub_type` = '" . $suffix . "', `eng_word_1`.`ID_root_word` = '" . $arrRootWord['ID_eng_word_1'] . "' WHERE `eng_word_1`.`ID_eng_word_1` = '" . $arrWord['ID_eng_word_1'] . "'";
					$objDB->query($stmt);
				}
				else {
					$stmt = "SELECT * FROM `eng_word_1`
					WHERE `eng_word_1`.`word` LIKE '" . substr($arrWord['word'], 0, strlen($arrWord['word']) - strlen($suffix)) . "%' AND `eng_word_1`.`word` != '" . $arrWord['word'] . "'";
					$arrWords = $objDB->select($stmt);
					if ($arrWords) {
						echo $arrWord['word'];
						echo '<pre>';
							print_r($arrWords);
						echo '</pre>';
					}
				}
			}
		}
		
		
	}
	
	/**
	 * 
	 * @param string $strSuffix - суффикс
	 */
	public function findWordsWithSuffix($strSuffix) {
		$stmt = "SELECT * FROM `eng_word_1` WHERE `eng_word_1`.`word` LIKE '%" . $strSuffix . "' AND `eng_word_1`.`ID_root_word` IS NULL";
		$objDB = Service_Database::instance();
		$arrWords = $objDB->select($stmt);
		return $arrWords;
		
	}
	
	/**
	 * Объединяет слова друг с другом по суффиксам
	 * 
	 */
	public function joinWordsBySuffixes() {
		$arrToDo = array();
		$arrToDo[0]['suffix'] = 'ly';
		$arrToDo[0]['alternative'] = 'le';
		$arrToDo[0]['alternative_2'] = 'l';
		$arrToDo[1]['suffix'] = 'ed';
		$arrToDo[1]['alternative'] = 'e';
		$arrToDo[1]['alternative_2'] = '';
		$arrToDo[2]['suffix'] = 's';
		$arrToDo[2]['alternative'] = '';
		$arrToDo[2]['alternative_2'] = '';
		foreach ($arrToDo as $arrData) {
			$arrWords = $this->findWordsWithSuffix($arrData['suffix']);
			foreach ($arrWords as $arrWord) {
				$this->checkWordForCoincidences($arrWord, $arrData['suffix'], $arrData['alternative'], $arrData['alternative_2']);
			}
		}
		//$this->checkWordsForCoincidences($arrWords);
	}
	
}
?>